// pages/sales/boss_ai/staff_act_code.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
var posterHandle = require("../../template/poster_handle.js");
var app = getApp();
Page(Object.assign({}, posterHandle, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    posterUrl:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    var ctx = this;
    app.getUserInfo(ctx, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.drawPoster();
    });
  },
  drawPoster: function () {
    let _this = this;
    let ctx = wx.createCanvasContext('myCanvas');
    let pxRatio = wx.getSystemInfoSync().screenWidth / 750;
     pxRatio *= 2;
    let canvasWidth = 680 * pxRatio, canvasHeight = 700 * pxRatio;
    wx.showLoading({
      title: '加载中',
    });
    let drawEle = [
      //背景
      { type: "rect", x: 0 * pxRatio, y: 0 * pxRatio, width: canvasWidth, height: canvasHeight, url: "", fontSize: 0, color: "#fff", text: "" },
      { type: "img", x: 90 * pxRatio, y: 40 * pxRatio, width: 500 * pxRatio, height: 500 * pxRatio, url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/getEmployeeWxCode.jpg?page=pages/bindsales/bind&cusmallToken=" + cusmallToken },
      { type: "txt", x: 249 * pxRatio, y: 600 * pxRatio, width: 350 * pxRatio, height: 40 * pxRatio, fontSize: 26 * pxRatio, color: "#222", text: "长按识别二维码" },
      { type: "txt", x: 262 * pxRatio, y: 650 * pxRatio, width: 350 * pxRatio, height: 40 * pxRatio, fontSize: 26 * pxRatio, color: "#222", text: "验证绑定信息" },
    ];
    _this.drawPostDetailCommon(ctx, drawEle, 0, canvasWidth, canvasHeight, function () {
      wx.hideLoading();
      wx.canvasToTempFilePath({
        canvasId: 'myCanvas',
        success: function (res) {
          _this.setData({ posterUrl: res.tempFilePath });
        }
      });
    });

  },
  saveImageToAlbum: function () {//保存海报图片到相册
    let _this = this;
    wx.showLoading({ title: '保存中' });
    wx.saveImageToPhotosAlbum({
      filePath: _this.data.posterUrl,
      success(res) {
        wx.hideLoading();
        wx.showModal({
          title: '保存海报成功',
          content: app.globalData.cardTitle+'海报已保存到手机相册，你可以分享到朋友圈了',
          showCancel: false,
          success: function (res) {
            wx.previewImage({
              current: _this.data.posterUrl, // 当前显示图片的http链接
              urls: [_this.data.posterUrl] // 需要预览的图片http链接列表
            });
          }
        });
      },
      fail(res) {
        wx.hideLoading();
        wx.openSetting({});
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))